import pandas as pd
import warnings
warnings.filterwarnings('ignore')

train = pd.read_csv('data/trn_per_300.csv', header=0)
tst = pd.read_csv('data/tst_per_300.csv', header=0)
cols = [a for a in train.columns if a not in ['type']]
A = pd.concat([train[cols], tst[cols]], axis=0)
features = [a for a in train.columns if a not in ['ship', 'type']]
A1 = A[features]

def pertile(a_in, n, drop_val=0):
    import copy
    import numpy as np
    import pandas as pd
    a = pd.Series(copy.deepcopy(a_in))
    a1 = a[a != drop_val]
    flag = np.ones(len(a1))
    b1 = copy.deepcopy(a1)
    p = np.linspace(0, 1, n + 1)
    a1p = a1.quantile(p[::-1][1:])
    # print(a1p)
    for i, ta in enumerate(a1p):
        a1[(b1 >= ta) & (flag == 1)] = n - i
        flag[(b1 >= ta) & (flag == 1)] = 0
    a[a != drop_val] = a1
    return a

n_level = 255
A1[features] = A1[features].apply(lambda x: pertile(x, n_level, 0), axis=0)

Atrn = A1.iloc[:7000, :]
Atrn['type'] = train['type']
Atrn.to_csv(f'data/trn_per_300_cnn_{n_level}.csv', header=True, index=False)

Atst = A1.iloc[7000:, :]
Atst.to_csv(f'data/tst_per_300_cnn_{n_level}.csv', header=True, index=False)